home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
-
- class SingletonException(Exception):
- pass
-
-
- class Singleton(object):
-
- def getInstance(cls, *lstArgs):
- if cls._isInstantiated():
- if len(lstArgs) != 0:
- raise SingletonException, 'If no supplied args, singleton must already be instantiated, or __init__ must require no args'
-
- elif cls._getConstructionArgCountNotCountingSelf() > 0 and len(lstArgs) <= 0:
- raise SingletonException, 'If the singleton requires __init__ args, supply them on first instantiation'
-
- instance = cls.__new__(cls)
- instance.__init__(*lstArgs)
- cls.cInstance = instance
- return cls.cInstance
-
- getInstance = classmethod(getInstance)
-
- def _isInstantiated(cls):
- return hasattr(cls, 'cInstance')
-
- _isInstantiated = classmethod(_isInstantiated)
-
- def _getConstructionArgCountNotCountingSelf(cls):
- return cls.__init__.im_func.func_code.co_argcount - 1
-
- _getConstructionArgCountNotCountingSelf = classmethod(_getConstructionArgCountNotCountingSelf)
-
- def _forgetClassInstanceReferenceForTesting(cls):
-
- try:
- delattr(cls, 'cInstance')
- except AttributeError:
- for baseClass in cls.__bases__:
- if issubclass(baseClass, Singleton):
- baseClass._forgetClassInstanceReferenceForTesting()
- continue
-
-
-
- _forgetClassInstanceReferenceForTesting = classmethod(_forgetClassInstanceReferenceForTesting)
-
- if __name__ == '__main__':
- import unittest
-
- class PublicInterfaceTest(unittest.TestCase):
-
- def testReturnsSameObject(self):
-
- class A((Singleton,)):
-
- def __init__(self):
- super(A, self).__init__()
-
-
- a1 = A.getInstance()
- a2 = A.getInstance()
- self.assertEquals(id(a1), id(a2))
-
-
- def testInstantiateWithMultiArgConstructor(self):
-
- class B((Singleton,)):
-
- def __init__(self, arg1, arg2):
- super(B, self).__init__()
- self.arg1 = arg1
- self.arg2 = arg2
-
-
- b1 = B.getInstance('arg1 value', 'arg2 value')
- b2 = B.getInstance()
- self.assertEquals(b1.arg1, 'arg1 value')
- self.assertEquals(b1.arg2, 'arg2 value')
- self.assertEquals(id(b1), id(b2))
-
-
- def testTryToInstantiateWithoutNeededArgs(self):
-
- class B((Singleton,)):
-
- def __init__(self, arg1, arg2):
- super(B, self).__init__()
- self.arg1 = arg1
- self.arg2 = arg2
-
-
- self.assertRaises(SingletonException, B.getInstance)
-
-
- def testTryToInstantiateWithoutGetInstance(self):
-
- class A((Singleton,)):
-
- def __init__(self):
- super(A, self).__init__()
-
-
- self.assertRaises(SingletonException, A)
-
-
- def testDontAllowNew(self):
-
- def instantiatedAnIllegalClass():
-
- class A((Singleton,)):
-
- def __init__(self):
- super(A, self).__init__()
-
-
- def __new__(metaclass, strName, tupBases, dict):
- return super(MetaSingleton, metaclass).__new__(metaclass, strName, tupBases, dict)
-
-
-
- self.assertRaises(SingletonException, instantiatedAnIllegalClass)
-
-
- def testDontAllowArgsAfterConstruction(self):
-
- class B((Singleton,)):
-
- def __init__(self, arg1, arg2):
- super(B, self).__init__()
- self.arg1 = arg1
- self.arg2 = arg2
-
-
- b1 = B.getInstance('arg1 value', 'arg2 value')
- self.assertRaises(SingletonException, B, 'arg1 value', 'arg2 value')
-
-
- def test_forgetClassInstanceReferenceForTesting(self):
-
- class A((Singleton,)):
-
- def __init__(self):
- super(A, self).__init__()
-
-
-
- class B((A,)):
-
- def __init__(self):
- super(B, self).__init__()
-
-
- a = A.getInstance()
- A._forgetClassInstanceReferenceForTesting()
- b = B.getInstance()
- B._forgetClassInstanceReferenceForTesting()
- a = A.getInstance()
- B._forgetClassInstanceReferenceForTesting()
- b = B.getInstance()
-
-
- unittest.main()
-
-